<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="input" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-if="kw.length === 0">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in historyList" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list">
      <view class="sugg-item" v-for="(item, i) in searchResult" :key="i" @click="gotoDetail(item.goods_id)">
       <view class="goods-name">{{item.goods_name}}</view>
       <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
  </view>
</template>

<script>
  export default {
    onLoad() {
      //加载搜索历史记录
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    data() {
      return {
        //延时器
        timer: null,
        //搜索关键词
        kw: '',
        //搜索结果
        searchResult: [],
        //搜索历史
        historyList: ['a','b','c']

      };
    },
    methods: {
      input(e) {
        //防抖
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          //输入框的值
          this.kw = e.value.trim()
          //判断输入框的值是否为空
          if (!kw) {
            return
          }
          //根据关键词搜索
          this.getSearchResult()

        }, 500)
      },
      //根据关键词搜索
      async getSearchResult() {
        const {  data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', {  query: this.kw  })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.searchResult = res.message
        //保存搜索历史记录
        this.saveSearchHistory()
      },
      //点击跳转到商品详情页
      gotoDetail(goods_id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
        })
      },
      //保存搜索历史记录
      saveSearchHistory(){
        const set = new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        //持久化存储
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      //清空搜索历史记录
      cleanHistory(){
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      //点击搜索历史记录跳转到商品列表
      gotoGoodsList(kw){
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
      

    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .sugg-list {
    padding: 0 5px;
    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      justify-content: space-between;
      .goods-name {
        //文字不允许换行
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
</style>